package BadBoy;

import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Frame;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.image.BufferedImage;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;

import javax.imageio.ImageIO;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.ImageIcon;
import javax.swing.JCheckBoxMenuItem;
import javax.swing.JComboBox;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLayeredPane;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPopupMenu;
import javax.swing.JScrollPane;
import javax.swing.JTabbedPane;
import javax.swing.JTextField;
import javax.swing.JTextPane;
import javax.swing.JTree;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.event.TreeSelectionEvent;
import javax.swing.event.TreeSelectionListener;
import javax.swing.filechooser.FileFilter;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeModel;
import javax.swing.tree.TreeSelectionModel;

import chrriis.common.UIUtils;
import chrriis.dj.nativeswing.swtimpl.NativeComponent;
import chrriis.dj.nativeswing.swtimpl.NativeInterface;
import chrriis.dj.nativeswing.swtimpl.components.JWebBrowser;
import chrriis.dj.nativeswing.swtimpl.components.WebBrowserAdapter;
import chrriis.dj.nativeswing.swtimpl.components.WebBrowserEvent;
import java.awt.event.ItemListener;
import java.awt.event.ItemEvent;
import chrriis.dj.nativeswing.swtimpl.components.WebBrowserNavigationEvent;

//import BadBoy.splashScreen;

public class BadBoy {
	private JFrame frmBadboy;
	private JLayeredPane panel_3;
//----------------Create a List Menu for event Mouse Right---------//
	JPopupMenu menu = new JPopupMenu("Popup");
//----------------Declare Timer use in Playback---------------------//
	Timer timer = new Timer();
//----------------Declare Webbrowser For Badboy---------------------//
	JWebBrowser webbrowser;
	JPanel EmbedBrowser;
	FileFilter fileFilter;
//----------------Declare a TreView --------------------------------//
	JTree Script;
	DefaultTreeModel model;
	DefaultMutableTreeNode Suite;
	DefaultMutableTreeNode Test;
	DefaultMutableTreeNode Step;
	DefaultMutableTreeNode NodeUrl;
	DefaultMutableTreeNode Parameter;
//----------------Declare Variables global---------------------------//
	int index = 0;
	int counttimer = 0;
	List<String> record = new ArrayList<String>();
	List<String> parameter = new ArrayList<String>();
	String checklooptext;
	String url;
	String disUrl;
	boolean counttitlechange = true;
	Boolean rightClick = false;
	Boolean AllowRecordUrl = false;
	Boolean AllowRecord = true;
	Boolean AllowChangeBrowser = false;
    Boolean AllowEnterUrl = false;
    Boolean check_Combobox = true;
	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		NativeInterface.open();
		UIUtils.setPreferredLookAndFeel();
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					BadBoy window = new BadBoy();
					window.frmBadboy.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
		NativeInterface.runEventPump();
	}

	/**
	 * Create the application.
	 */
	public BadBoy() {
		// splash.splashInit();
		// splash.appInit();
		// if (splash.mySplash != null) // check if we really had a spashscreen
		// splash.mySplash.close(); // we're done with it
		initialize();
		
	}

	/**
	 * Initialize the contents of the frame.
	 */
	private void initialize() {
//		final Xml xml = new Xml();
		frmBadboy = new JFrame();
		frmBadboy.setIconImage(Toolkit.getDefaultToolkit().getImage(BadBoy.class.getResource("/icons/BadBoy.png")));
		frmBadboy.setTitle("BadBoy (Recording)");
		frmBadboy.setBounds(100, 100, 715, 424);
		frmBadboy.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frmBadboy.setExtendedState(Frame.MAXIMIZED_BOTH);
		panel_3 = new JLayeredPane();
//_________________________Click Mouse Right________________________________//
		JMenuItem Item = new JMenuItem("Play Back");
		menu.add(Item);
//_________________________Textbox Test Result______________________________//
		JPanel panel_4 = new JPanel();
		final JTextPane textPane = new JTextPane();
//_________________________Create MenuBar Tool______________________________//
		JMenuBar menuBar = new JMenuBar();
		JMenu mnFile = new JMenu("File");
		JMenu mnNewMenu_1 = new JMenu("Edit");
		JMenu mnNewMenu_2 = new JMenu("View");
		JMenu mnTool = new JMenu("Tools");
		JMenu mnPreferences = new JMenu("Preferences");
		JMenu mnHepl = new JMenu("Help");
//_________________________Menu Bar add Icon new,save..._____________________//
		JMenuBar menuBar_ex = new JMenuBar();
		final JMenu news = new JMenu("");
		news.setToolTipText("New");
		news.setIcon(new ImageIcon(
				BadBoy.class
						.getResource("/fckeditor/editor/filemanager/browser/default/images/icons/default.icon.gif")));
		menuBar_ex.add(news);
		JMenu Open = new JMenu("");
		Open.setIcon(new ImageIcon(
				BadBoy.class
						.getResource("/fckeditor/editor/filemanager/browser/default/images/FolderOpened.gif")));
		Open.setToolTipText("Open");
		menuBar_ex.add(Open);
		JMenu Save = new JMenu("");
		Save.setIcon(new ImageIcon(BadBoy.class.getResource("/icons/save.jpg")));
		Save.setToolTipText("Save");
		menuBar_ex.add(Save);
		JMenu NewSuite = new JMenu("");
		NewSuite.setIcon(new ImageIcon(BadBoy.class
				.getResource("/icons/NewSuite.png")));
		NewSuite.setToolTipText("Create a New Suite");
		menuBar_ex.add(NewSuite);
		JMenu NewTest = new JMenu("");
		NewTest.setIcon(new ImageIcon(BadBoy.class
				.getResource("/icons/NewTest.png")));
		NewTest.setToolTipText("Create a New Test");
		menuBar_ex.add(NewTest);
		JMenu NewStep = new JMenu("");
		NewStep.setIcon(new ImageIcon(BadBoy.class
				.getResource("/icons/NewStep.png")));
		NewStep.setToolTipText("Create New Step");
		menuBar_ex.add(NewStep);	
		final JMenu Record = new JMenu("");
		Record.setIcon(new ImageIcon(BadBoy.class
				.getResource("/icons/onRecord.png")));
		Record.setToolTipText("Record");
		menuBar_ex.add(Record);	
		JMenu screenshoot = new JMenu("");
		screenshoot.setToolTipText("Capture Screen Shoot");	
		screenshoot.setIcon(new ImageIcon(BadBoy.class
				.getResource("/icons/screenshoot.png")));
		menuBar_ex.add(screenshoot);	
		final JMenu explorer = new JMenu("");
		explorer.setIcon(new ImageIcon(BadBoy.class
				.getResource("/icons/IE7.png")));
		menuBar_ex.add(explorer);
		final JMenu mozilla = new JMenu("");
		mozilla.setIcon(new ImageIcon(BadBoy.class
				.getResource("/icons/offMozilla.png")));
		menuBar_ex.add(mozilla);
//________________________interaction MenuBar Url_______________________________________//
		JPanel panel_2 = new JPanel();
		JMenuBar menuUrl = new JMenuBar();
		JMenu BackWard = new JMenu("");
		BackWard.setToolTipText("Back");
		BackWard.setIcon(new ImageIcon(
				BadBoy.class
						.getResource("/chrriis/dj/nativeswing/swtimpl/components/resource/nav_backward.gif")));
		menuUrl.add(BackWard);
		JMenu Forward = new JMenu("");
		Forward.setToolTipText("Forward");
		Forward.setIcon(new ImageIcon(
				BadBoy.class
						.getResource("/chrriis/dj/nativeswing/swtimpl/components/resource/nav_forward.gif")));
		menuUrl.add(Forward);
		JMenu Refresh = new JMenu("");
		Refresh.setToolTipText("Refresh");
		Refresh.setIcon(new ImageIcon(
				BadBoy.class
						.getResource("/chrriis/dj/nativeswing/swtimpl/components/resource/nav_refresh.gif")));
		menuUrl.add(Refresh);
		JMenu Stop = new JMenu("");
		Stop.setToolTipText("Stop");
		Stop.setIcon(new ImageIcon(
				BadBoy.class
						.getResource("/chrriis/dj/nativeswing/swtimpl/components/resource/nav_stop.gif")));
		menuUrl.add(Stop);
//________________________Create Text For Url___________________________________//
        final JComboBox<String> comboboxUrl = new JComboBox<String>();
        comboboxUrl.setEditable(true);
        menuUrl.add(comboboxUrl);
		JMenu enter = new JMenu("");
		enter.setIcon(new ImageIcon(
				BadBoy.class
						.getResource("/chrriis/dj/nativeswing/swtimpl/components/resource/nav_go.gif")));
		menuUrl.add(enter); 
//________________________Display Content WebBrowser____________________________//		
		JPanel panel = new JPanel();
		EmbedBrowser = new JPanel();
//_______________________Create Webbrowser______________________________________//
		webbrowser = new JWebBrowser();
		webbrowser.setBarsVisible(false);
		webbrowser.setStatusBarVisible(true);
		webbrowser.navigate("google.com.vn");		
//_______________________Display Treview_________________________________________//
		JPanel panel_1 = new JPanel();
		JScrollPane scrollPane = new JScrollPane();
		final JTabbedPane tabbedPane = new JTabbedPane(JTabbedPane.TOP);
//_______________________Create TreeView_________________________________________//
		Script = new JTree();
		model = (DefaultTreeModel) Script.getModel();
		Script.getSelectionModel().setSelectionMode(
				TreeSelectionModel.SINGLE_TREE_SELECTION);
//_______________________Capture event Create New Suite__________________________//		
		NewSuite.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent arg0) {
				Suite = new DefaultMutableTreeNode("Suite 1");
				model.setRoot(Suite);
				tabbedPane.add(Script);
			}
		});
//_______________________Capture event Create New Test___________________________//			
		NewTest.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent arg0) {
				if (Suite != null) {
					Test = new DefaultMutableTreeNode("Test 1");
					Step = new DefaultMutableTreeNode("Step 1");
					Test.add(Step);
					Suite.add(Test);
					model.reload();
					expandAll(Script);
				}
			}
		});
//_______________________Capture event Create New Step___________________________//     
		NewStep.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				if (Test != null) {
					Step = new DefaultMutableTreeNode("Step 1");
					Test.add(Step);
					Suite.add(Test);
					model.reload();
					expandAll(Script);
				}
			}
		});
//_______________________Capture event Create New Parameter___________________________//     
//		NewStep.addMouseListener(new MouseAdapter() {
//			@Override
//			public void mouseClicked(MouseEvent e) {
//				if (Step != null) {
//					Step = new DefaultMutableTreeNode("Step 1");
//					Test.add(Step);
//					Suite.add(Test);
//					model.reload();
//					expandAll(Script);
//				}
//			}
//		});
//_______________________Capture event change Browser Internet Explorer_______________________//	
		explorer.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent arg0) {
				if (AllowChangeBrowser == true) {
					mozilla.setIcon(new ImageIcon(BadBoy.class
							.getResource("/icons/offMozilla.png")));
					explorer.setIcon(new ImageIcon(BadBoy.class
							.getResource("/icons/IE7.png")));;
					AllowChangeBrowser = false;
					SwingUtilities.invokeLater(new Runnable() {
						@Override
						public void run() {
							webbrowser = new JWebBrowser();
							webbrowser.setBarsVisible(false);
							webbrowser.setStatusBarVisible(true);
							EmbedBrowser.add(webbrowser);
							webbrowser.navigate(url);
							counttitlechange = true;
							webbrowser.addWebBrowserListener(new WebBrowserAdapter() {
										@Override
										public void titleChanged(
												WebBrowserEvent e) {
											url = webbrowser
													.getResourceLocation();
											url = webbrowser
													.getResourceLocation();
											comboboxUrl.setSelectedItem(url);
											if (counttitlechange == true) {
												counttitlechange = false;
												checklooptext = url;
												AllowRecordUrl = true;
											}
											if (!url.equalsIgnoreCase(checklooptext)
													| AllowEnterUrl == true) {
												AllowRecordUrl = true;
												checklooptext = url;
												AllowEnterUrl = false;
											} else
												AllowRecordUrl = false;
											if (AllowRecord == true) {
												if ((Step != null)
														&& (AllowRecordUrl == true)) {
													record.add(url);
													Step.add(new DefaultMutableTreeNode(
															url));
													model.reload(Suite);
													expandAll(Script);
													AllowRecordUrl = false;
												}

											}
										}
									});
						}
					});
				} else {
					AllowChangeBrowser = true;
					mozilla.setIcon(new ImageIcon(BadBoy.class
							.getResource("/icons/Mozilla.png")));
					explorer.setIcon(new ImageIcon(BadBoy.class
							.getResource("/icons/offIE7.png")));;
					SwingUtilities.invokeLater(new Runnable() {
						@Override
						public void run() {
							webbrowser = new JWebBrowser(JWebBrowser
									.useXULRunnerRuntime());
							webbrowser.setBarsVisible(false);
							webbrowser.setStatusBarVisible(true);
							EmbedBrowser.add(webbrowser);
							webbrowser.navigate(url);
							counttitlechange = true;
							webbrowser
									.addWebBrowserListener(new WebBrowserAdapter() {
										@Override
										public void titleChanged(
												WebBrowserEvent e) {
											url = webbrowser
													.getResourceLocation();
											comboboxUrl.setSelectedItem(url);
											if (counttitlechange == true) {
												counttitlechange = false;
												checklooptext = url;
												AllowRecordUrl = true;
											}
											if (!url.equalsIgnoreCase(checklooptext)
													| AllowEnterUrl == true) {
												AllowRecordUrl = true;
												checklooptext = url;
												AllowEnterUrl = false;
											} else
												AllowRecordUrl = false;
											if (AllowRecord == true) {
												if ((Step != null)
														&& (AllowRecordUrl == true)) {
													record.add(url);
													Step.add(new DefaultMutableTreeNode(
															url));
													model.reload(Suite);
													expandAll(Script);
													AllowRecordUrl = false;
												}

											}
										}
									});
						}
					});
				}
			}
		});
//_______________________Capture event change Browser Internet Mozilla_______________________//
		mozilla.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent arg0) {
				if (AllowChangeBrowser == true) {
					mozilla.setIcon(new ImageIcon(BadBoy.class
							.getResource("/icons/offMozilla.png")));
					explorer.setIcon(new ImageIcon(BadBoy.class
							.getResource("/icons/IE7.png")));;
					AllowChangeBrowser = false;
					SwingUtilities.invokeLater(new Runnable() {
						@Override
						public void run() {
							webbrowser = new JWebBrowser();
							webbrowser.setBarsVisible(false);
							webbrowser.setStatusBarVisible(true);
							EmbedBrowser.add(webbrowser);
							webbrowser.navigate(url);
							counttitlechange = true;
							webbrowser.addWebBrowserListener(new WebBrowserAdapter() {
										@Override
										public void titleChanged(
												WebBrowserEvent e) {
											url = webbrowser
													.getResourceLocation();
											url = webbrowser
													.getResourceLocation();
											comboboxUrl.setSelectedItem(url);
											if (counttitlechange == true) {
												counttitlechange = false;
												checklooptext = url;
												AllowRecordUrl = true;
											}
											if (!url.equalsIgnoreCase(checklooptext)
													| AllowEnterUrl == true) {
												AllowRecordUrl = true;
												checklooptext = url;
												AllowEnterUrl = false;
											} else
												AllowRecordUrl = false;
											if (AllowRecord == true) {
												if ((Step != null)
														&& (AllowRecordUrl == true)) {
													record.add(url);
													Step.add(new DefaultMutableTreeNode(
															url));
													model.reload(Suite);
													expandAll(Script);
													AllowRecordUrl = false;
												}

											}
										}
									});
						}
					});
				} else {
					AllowChangeBrowser = true;
					mozilla.setIcon(new ImageIcon(BadBoy.class
							.getResource("/icons/Mozilla.png")));
					explorer.setIcon(new ImageIcon(BadBoy.class
							.getResource("/icons/offIE7.png")));;
					SwingUtilities.invokeLater(new Runnable() {
						@Override
						public void run() {
							webbrowser = new JWebBrowser(JWebBrowser
									.useXULRunnerRuntime());
							webbrowser.setBarsVisible(false);
							webbrowser.setStatusBarVisible(true);
							EmbedBrowser.add(webbrowser);
							webbrowser.navigate(url);
							counttitlechange = true;
							webbrowser
									.addWebBrowserListener(new WebBrowserAdapter() {
										@Override
										public void titleChanged(
												WebBrowserEvent e) {
											url = webbrowser
													.getResourceLocation();
											comboboxUrl.setSelectedItem(url);
											if (counttitlechange == true) {
												counttitlechange = false;
												checklooptext = url;
												AllowRecordUrl = true;
											}
											if (!url.equalsIgnoreCase(checklooptext)
													| AllowEnterUrl == true) {
												AllowRecordUrl = true;
												checklooptext = url;
												AllowEnterUrl = false;
											} else
												AllowRecordUrl = false;
											if (AllowRecord == true) {
												if ((Step != null)
														&& (AllowRecordUrl == true)) {
													record.add(url);
													Step.add(new DefaultMutableTreeNode(
															url));
													model.reload(Suite);
													expandAll(Script);
													AllowRecordUrl = false;
												}

											}
										}
									});
						}
					});
				}
			}
		});
//_______________________Select Node in TreView__________________________________//      
		Script.addTreeSelectionListener(new TreeSelectionListener() {

			@Override
			public void valueChanged(TreeSelectionEvent arg0) {
				DefaultMutableTreeNode node = (DefaultMutableTreeNode) Script
						.getLastSelectedPathComponent();
				if (node == null)
					return;
				if (node == Suite) {
					rightClick = true;
				} else {
					rightClick = false;
				}
			}
		});
//_______________________Capture event right mouse in TreView____________________//			
		Script.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				if ((SwingUtilities.isRightMouseButton(e))
						&& (rightClick == true)) {
					menu.show(e.getComponent(), e.getX(), e.getY());

				}
			}
		});
//_______________________Execute Event Play Back_________________________________//			
		Item.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent arg0) {
				timer = new Timer();
				counttimer = record.size();
				frmBadboy.setTitle("BadBoy");
				AllowRecord = false;
				Record.setIcon(new ImageIcon(BadBoy.class
						.getResource("/icons/Record.png")));
				timer.schedule(new TimerTask() {
					@Override
					public void run() {
						disUrl = record.get(index);
						textPane.setText("" + index);
						SwingUtilities.invokeLater(new Runnable() {
							@Override
							public void run() {
								webbrowser.navigate(disUrl);
							}
						});
						index++;
						if (index == counttimer) {
							index = 0;
							textPane.setText("playback sucessfull");
							timer.cancel();
							timer.purge();
							return;
						}
					}
				}, 0, 2 * 60 * 100);

			}
		});
//_______________________Capture event Record_______________________//			
		Record.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent arg0) {
				if (AllowRecord == true) {
					frmBadboy.setTitle("BadBoy");
					AllowRecord = false;
					Record.setIcon(new ImageIcon(BadBoy.class
							.getResource("/icons/Record.png")));
				} else {
					frmBadboy.setTitle("BadBoy (Recording)");
					AllowRecord = true;
					Record.setIcon(new ImageIcon(BadBoy.class
							.getResource("/icons/onRecord.png")));;
				}
				timer.cancel();

			}
		});
//_______________________Capture event change Url______________________//	
		final JTextPane texturlcontent = new JTextPane();
		
		webbrowser.addWebBrowserListener(new WebBrowserAdapter() {
			@Override
			public void titleChanged(WebBrowserEvent e) {
				url = webbrowser.getResourceLocation();
				comboboxUrl.setSelectedItem(url);
				if (counttitlechange == true) {
					counttitlechange = false;
					checklooptext = url;
					AllowRecordUrl = true;
				}
				if (!url.equalsIgnoreCase(checklooptext)
						| AllowEnterUrl == true) {
					AllowRecordUrl = true;
					checklooptext = url;
					AllowEnterUrl = false;

				} else
					AllowRecordUrl = false;
				if (AllowRecord == true) {
					if ((Step != null) && (AllowRecordUrl == true)) {
						record.add(url);
						NodeUrl = new DefaultMutableTreeNode(url);
						Step.add(NodeUrl);
						if(url.contains("#"))
						{
							String[] removecharater = url.split("#");
							String[] part = removecharater[1].split("&");
							for(String s : part)
							{
								Parameter = new DefaultMutableTreeNode(s);
								NodeUrl.add(Parameter);
							}
						}
						model.reload(Suite);
						expandAll(Script);
						AllowRecordUrl = false;
//						texturlcontent.setText(webbrowser.getHTMLContent());
					}

				}
			}
			@Override
			public void loadingProgressChanged(WebBrowserEvent e) {
//				System.out.println(e.getWebBrowser().getLoadingProgress());
			}
			@Override
			public void statusChanged(WebBrowserEvent e) {
//				System.out.println(e.getWebBrowser().getLoadingProgress());
			}
			@Override
			public void locationChanged(WebBrowserNavigationEvent e) {
//				System.out.println(e.getWebBrowser().getLoadingProgress());
			}
		});
//_______________________Capture event Backward________________________//		
		BackWard.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent arg0) {
				webbrowser.navigateBack();
			}
		});
//_______________________Capture event Forward__________________________//		
		Forward.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent arg0) {
				webbrowser.navigateForward();
			}
		});
//_______________________Capture event Refresh__________________________//		
		Refresh.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent arg0) {
				if (url != null) {
					webbrowser.navigate(url);
				}
			}
		});
//_______________________Capture event stop_____________________________//		
		Stop.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent arg0) {
				webbrowser.stopLoading();
			}
		});
//_______________________Capture event Enter Url_________________________//	
		comboboxUrl.getEditor().getEditorComponent().addKeyListener(new KeyAdapter(){
			private Boolean found = false;
			@Override
			 public void keyReleased(KeyEvent event) {
		        if (event.getKeyChar() == KeyEvent.VK_ENTER) {
		        	if(!comboboxUrl.getSelectedItem().toString().equals(""))
		        	{
		        		url = comboboxUrl.getSelectedItem().toString();
						webbrowser.navigate(url);
						AllowEnterUrl = true;
		                String s = (String) comboboxUrl.getSelectedItem();
	                    for (int i = 0; i < comboboxUrl.getItemCount(); i++) {
	                        if (comboboxUrl.getItemAt(i).toString().equals(s)) {
	                            found = true;
	                            break;
	                        }
	                    }
	                    if (!found) {
	                        System.out.println("Added: " + s);
	                        comboboxUrl.addItem(s);
	                    }
	                    found = false;
		        	}
		        }
			}
		});
//_______________________Capture event arrow_____________________________//
		enter.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent arg0) {
				if (url != null) {
					webbrowser.navigate(url);
				}
			}
		});
//_______________________Capture event Screen SHoot_______________________//
		final String LS = System.getProperty("line.separator");
		screenshoot.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent arg0) {
				String result = (String) webbrowser
						.executeJavascriptWithResult("var width = 0;"
								+ LS
								+ "var height = 0;"
								+ LS
								+ "if(document.documentElement) {"
								+ LS
								+ "  width = Math.max(width, document.documentElement.scrollWidth);"
								+ LS
								+ "  height = Math.max(height, document.documentElement.scrollHeight);"
								+ LS
								+ "}"
								+ LS
								+ "if(self.innerWidth) {"
								+ LS
								+ "  width = Math.max(width, self.innerWidth);"
								+ LS
								+ "  height = Math.max(height, self.innerHeight);"
								+ LS
								+ "}"
								+ LS
								+ "if(document.body.scrollWidth) {"
								+ LS
								+ "  width = Math.max(width, document.body.scrollWidth);"
								+ LS
								+ "  height = Math.max(height, document.body.scrollHeight);"
								+ LS + "}" + LS
								+ "return width + '/' + height;");
				// This may happen from time to time so we have to fail
				// gracefully.
				int index = result == null ? -1 : result.indexOf("/");
				if (index < 0) {
					JOptionPane.showMessageDialog(webbrowser,
							"An error occurred while capturing the full-page",
							"Full-page capture failure",
							JOptionPane.ERROR_MESSAGE);
				} else {
					NativeComponent nativeComponent = webbrowser
							.getNativeComponent();
					Dimension originalSize = nativeComponent.getSize();
					Dimension imageSize = new Dimension(Integer.parseInt(result
							.substring(0, index)), Integer.parseInt(result
							.substring(index + 1)));
					// We add some artificial spacing because with scrollbars
					// logic it is likely to be wrong...
					imageSize.width = Math.max(originalSize.width,
							imageSize.width + 50);
					imageSize.height = Math.max(originalSize.height,
							imageSize.height + 50);
					nativeComponent.setSize(imageSize);
					BufferedImage image = new BufferedImage(imageSize.width,
							imageSize.height, BufferedImage.TYPE_INT_RGB);
					nativeComponent.paintComponent(image);
					nativeComponent.setSize(originalSize);
					ScreenShoot transferable = new ScreenShoot(image);
					Toolkit.getDefaultToolkit().getSystemClipboard()
							.setContents(transferable, null);

					JFileChooser jf = new JFileChooser(getClass()
							.getProtectionDomain().getCodeSource()
							.getLocation().getPath());
					jf.setDialogTitle("Save As");
					fileFilter = new FileFilter() {

						@Override
						public String getDescription() {
							return "PNG (*.png)";
						}

						@Override
						public boolean accept(File arg0) {
							return (arg0.getName().endsWith(".png") || arg0
									.isDirectory());
						}
					};
					jf.setFileFilter(fileFilter);
					Component modalToComponent = null;
					if (jf.showSaveDialog(modalToComponent) == JFileChooser.APPROVE_OPTION) {
						File f = jf.getSelectedFile();
						f = new File(f.getPath() + ".png");
						try {
							ImageIO.write(image, "png", f);
						} catch (Exception e) {
							JFrame frame = new JFrame(
									"JOptionPane showMessageDialog example");
							JOptionPane.showMessageDialog(frame, "Error!",
									"Error!", JOptionPane.ERROR_MESSAGE);
						}
					}

				}
			}
		});
		menuBar_ex.setBackground(UIManager.getColor("CheckBox.background"));
		GroupLayout gl_panel_2 = new GroupLayout(panel_2);
		gl_panel_2.setHorizontalGroup(gl_panel_2.createParallelGroup(
				Alignment.LEADING).addComponent(menuUrl,
				GroupLayout.DEFAULT_SIZE, 699, Short.MAX_VALUE));
		gl_panel_2.setVerticalGroup(gl_panel_2.createParallelGroup(
				Alignment.LEADING).addComponent(menuUrl,
				GroupLayout.DEFAULT_SIZE, 33, Short.MAX_VALUE));
		panel_2.setLayout(gl_panel_2);
		GroupLayout gl_panel_3 = new GroupLayout(panel_3);
		gl_panel_3.setHorizontalGroup(gl_panel_3.createParallelGroup(
				Alignment.LEADING).addComponent(menuBar_ex,
				GroupLayout.DEFAULT_SIZE, 699, Short.MAX_VALUE));
		gl_panel_3.setVerticalGroup(gl_panel_3.createParallelGroup(
				Alignment.LEADING).addGroup(
				gl_panel_3
						.createSequentialGroup()
						.addComponent(menuBar_ex, GroupLayout.PREFERRED_SIZE,
								23, GroupLayout.PREFERRED_SIZE)
						.addContainerGap(GroupLayout.DEFAULT_SIZE,
								Short.MAX_VALUE)));	
		panel_3.setLayout(gl_panel_3);
		GroupLayout gl_panel_1 = new GroupLayout(panel_1);
		gl_panel_1.setHorizontalGroup(gl_panel_1.createParallelGroup(
				Alignment.LEADING).addComponent(scrollPane,
				GroupLayout.DEFAULT_SIZE, 155, Short.MAX_VALUE));
		gl_panel_1.setVerticalGroup(gl_panel_1.createParallelGroup(
				Alignment.LEADING).addComponent(scrollPane,
				GroupLayout.DEFAULT_SIZE, 176, Short.MAX_VALUE));
		scrollPane.setViewportView(tabbedPane);
		panel_1.setLayout(gl_panel_1);
		panel.setLayout(new BorderLayout(0, 0));
		GroupLayout groupLayout = new GroupLayout(frmBadboy.getContentPane());
		groupLayout.setHorizontalGroup(
			groupLayout.createParallelGroup(Alignment.LEADING)
				.addComponent(panel_3)
				.addGroup(groupLayout.createSequentialGroup()
					.addComponent(panel_4, GroupLayout.PREFERRED_SIZE, 148, GroupLayout.PREFERRED_SIZE)
					.addPreferredGap(ComponentPlacement.RELATED)
					.addComponent(panel, GroupLayout.DEFAULT_SIZE, 545, Short.MAX_VALUE))
				.addComponent(panel_1, GroupLayout.PREFERRED_SIZE, 155, GroupLayout.PREFERRED_SIZE)
				.addComponent(panel_2, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
		);
		groupLayout.setVerticalGroup(
			groupLayout.createParallelGroup(Alignment.LEADING)
				.addGroup(groupLayout.createSequentialGroup()
					.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
						.addGroup(groupLayout.createSequentialGroup()
							.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
								.addComponent(panel_3, GroupLayout.PREFERRED_SIZE, 23, GroupLayout.PREFERRED_SIZE)
								.addGroup(groupLayout.createSequentialGroup()
									.addGap(46)
									.addComponent(panel_1, GroupLayout.PREFERRED_SIZE, 176, GroupLayout.PREFERRED_SIZE))
								.addGroup(groupLayout.createSequentialGroup()
									.addGap(22)
									.addComponent(panel_2, GroupLayout.PREFERRED_SIZE, 25, GroupLayout.PREFERRED_SIZE)))
							.addPreferredGap(ComponentPlacement.RELATED, 9, Short.MAX_VALUE)
							.addComponent(panel_4, GroupLayout.PREFERRED_SIZE, 133, GroupLayout.PREFERRED_SIZE))
						.addGroup(groupLayout.createSequentialGroup()
							.addGap(46)
							.addComponent(panel, GroupLayout.DEFAULT_SIZE, 318, Short.MAX_VALUE)))
					.addGap(0))
		);
		
		
		GroupLayout gl_panel_4 = new GroupLayout(panel_4);
		gl_panel_4.setHorizontalGroup(
			gl_panel_4.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_panel_4.createSequentialGroup()
					.addGap(26)
					.addGroup(gl_panel_4.createParallelGroup(Alignment.TRAILING, false)
						.addComponent(texturlcontent, Alignment.LEADING)
						.addComponent(textPane, Alignment.LEADING, GroupLayout.DEFAULT_SIZE, 95, Short.MAX_VALUE))
					.addGap(27))
		);
		gl_panel_4.setVerticalGroup(
			gl_panel_4.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_panel_4.createSequentialGroup()
					.addGap(20)
					.addComponent(textPane, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
					.addGap(18)
					.addComponent(texturlcontent, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
					.addContainerGap(55, Short.MAX_VALUE))
		);
		panel_4.setLayout(gl_panel_4);
		EmbedBrowser.setLayout(new BorderLayout(0, 0));
		EmbedBrowser.add(webbrowser);
		panel.add(EmbedBrowser, BorderLayout.CENTER);
		frmBadboy.getContentPane().setLayout(groupLayout);
		frmBadboy.setJMenuBar(menuBar);
		menuBar.add(mnFile);
		JMenuItem mntmNew = new JMenuItem("New                 Ctrl+N");
		mnFile.add(mntmNew);
		JMenuItem mntmF = new JMenuItem("Open...            Ctrl+O");
		mnFile.add(mntmF);
		JMenuItem mntmNewMenuItem = new JMenuItem("Save                 Ctrl+S");
		mnFile.add(mntmNewMenuItem);
		JMenu mnNewMenu = new JMenu("Save As");
		mnFile.add(mnNewMenu);
		JMenuItem mntmNewBadboyScript = new JMenuItem("New BadBoy Script");
		mnNewMenu.add(mntmNewBadboyScript);
		JMenuItem mntmNewMenuItem_5 = new JMenuItem("Xml...");
		mnNewMenu.add(mntmNewMenuItem_5);
		JMenuItem mntmNewMenuItem_6 = new JMenuItem("Summary Report");
		mnNewMenu.add(mntmNewMenuItem_6);
		JMenuItem mntmNewMenuItem_7 = new JMenuItem("HTML Report ...");
		mnNewMenu.add(mntmNewMenuItem_7);
		JMenuItem mntmHtmlDocumentation = new JMenuItem(
				"HTML Documentation ...");
		mnNewMenu.add(mntmHtmlDocumentation);
		JMenuItem mntmNewMenuItem_1 = new JMenuItem("Export Respone Times");
		mnFile.add(mntmNewMenuItem_1);
		JMenuItem mntmNewMenuItem_2 = new JMenuItem("Export to JMeter...");
		mnFile.add(mntmNewMenuItem_2);
		JMenuItem mntmNewMenuItem_3 = new JMenuItem("Script Properties...");
		mnFile.add(mntmNewMenuItem_3);
		JMenuItem mntmNewMenuItem_4 = new JMenuItem(
				"Print                Ctrl+P");
		mnFile.add(mntmNewMenuItem_4);
		JMenuItem mntmPrintSetup = new JMenuItem("Print Setup");
		mnFile.add(mntmPrintSetup);
		JMenuItem mntmExit = new JMenuItem("Exit");
		mnFile.add(mntmExit);
		menuBar.add(mnNewMenu_1);
		JMenuItem mntmUndoCtrlz = new JMenuItem("Undo               Ctrl+Z");
		mnNewMenu_1.add(mntmUndoCtrlz);
		JMenuItem mntmRedo = new JMenuItem("Redo                Ctrl+Y");
		mnNewMenu_1.add(mntmRedo);
		JMenuItem mntmCutCtrlc = new JMenuItem("Cut                  Ctrl+X");
		mnNewMenu_1.add(mntmCutCtrlc);
		JMenuItem mntmCopyCtrlc = new JMenuItem("Copy                Ctrl+C");
		mnNewMenu_1.add(mntmCopyCtrlc);
		JMenuItem mntmPasteCtrlp = new JMenuItem("Paste                Ctrl+V");
		mnNewMenu_1.add(mntmPasteCtrlp);
		JMenuItem mntmSearchreplace = new JMenuItem("Search/Replace     F3");
		mnNewMenu_1.add(mntmSearchreplace);
		menuBar.add(mnNewMenu_2);
		JCheckBoxMenuItem chckbxmntmToolbar = new JCheckBoxMenuItem("ToolBar");
		mnNewMenu_2.add(chckbxmntmToolbar);
		JCheckBoxMenuItem chckbxmntmStatusBar = new JCheckBoxMenuItem(
				"Status Bar");
		mnNewMenu_2.add(chckbxmntmStatusBar);
		JCheckBoxMenuItem chckbxmntmBrowserBar = new JCheckBoxMenuItem(
				"Browser Bar");
		mnNewMenu_2.add(chckbxmntmBrowserBar);
		JCheckBoxMenuItem chckbxmntmScriptTreeF = new JCheckBoxMenuItem(
				"Script Tree               F8");
		mnNewMenu_2.add(chckbxmntmScriptTreeF);
		JCheckBoxMenuItem chckbxmntmProblemAnnotions = new JCheckBoxMenuItem(
				"Problem Annotions");
		mnNewMenu_2.add(chckbxmntmProblemAnnotions);
		JCheckBoxMenuItem chckbxmntmSummaryViewF = new JCheckBoxMenuItem(
				"Summary View     F9");
		mnNewMenu_2.add(chckbxmntmSummaryViewF);
		JCheckBoxMenuItem chckbxmntmCokies = new JCheckBoxMenuItem("Cookies...");
		mnNewMenu_2.add(chckbxmntmCokies);
		JMenu mnReport = new JMenu("Report");
		mnNewMenu_2.add(mnReport);
		JCheckBoxMenuItem chckbxmntmDatailReport = new JCheckBoxMenuItem(
				"Datail Report");
		mnReport.add(chckbxmntmDatailReport);
		JCheckBoxMenuItem chckbxmntmSummaryReport = new JCheckBoxMenuItem(
				"Summary Report");
		mnReport.add(chckbxmntmSummaryReport);
		JCheckBoxMenuItem chckbxmntmReviewReport = new JCheckBoxMenuItem(
				"Review Report");
		mnReport.add(chckbxmntmReviewReport);
		JCheckBoxMenuItem chckbxmntmServerLogFile = new JCheckBoxMenuItem(
				"Server Log File");
		mnNewMenu_2.add(chckbxmntmServerLogFile);
		JCheckBoxMenuItem chckbxmntmDomWindown = new JCheckBoxMenuItem(
				"DOM Window");
		mnNewMenu_2.add(chckbxmntmDomWindown);
		JCheckBoxMenuItem chckbxmntmVariales = new JCheckBoxMenuItem(
				"Variables                              Ctrl-Alt-V");
		mnNewMenu_2.add(chckbxmntmVariales);
		JCheckBoxMenuItem chckbxmntmToolsCtrlt = new JCheckBoxMenuItem(
				"Tools                                     Ctrl-T");
		mnNewMenu_2.add(chckbxmntmToolsCtrlt);
		JCheckBoxMenuItem chckbxmntmToggleViewOnoff = new JCheckBoxMenuItem(
				"Toggle View On/Off              F12");
		mnNewMenu_2.add(chckbxmntmToggleViewOnoff);
		JCheckBoxMenuItem chckbxmntmShowAllViews = new JCheckBoxMenuItem(
				"Show All Views");
		mnNewMenu_2.add(chckbxmntmShowAllViews);
		JCheckBoxMenuItem chckbxmntmScno = new JCheckBoxMenuItem(
				"Sychronize       Content");
		mnNewMenu_2.add(chckbxmntmScno);	
		menuBar.add(mnTool);
		JMenuItem mntmNewMenuItem_8 = new JMenuItem(
				"Create Hostname Variable...");
		mnTool.add(mntmNewMenuItem_8);
		JMenuItem mntmRunBackground = new JMenuItem("Run Background Threads...");
		mnTool.add(mntmRunBackground);
		JMenu mnDataSource = new JMenu("Data Source");
		mnTool.add(mnDataSource);
		JMenuItem mntmAttachVariableSource = new JMenuItem(
				"Attach Variable Source...");
		mnDataSource.add(mntmAttachVariableSource);
		JMenuItem mntmResetDataSource = new JMenuItem("Reset Data Source");
		mnDataSource.add(mntmResetDataSource);
		JMenuItem mntmDataSourceSetting = new JMenuItem(
				"Data Source Settings...");
		mnDataSource.add(mntmDataSourceSetting);
		JMenuItem mntmClear = new JMenuItem("Clear");
		mnDataSource.add(mntmClear);
		JMenuItem mntmClearResponses = new JMenuItem(
			"Clear Responses             Ctrl-K");
		mnTool.add(mntmClearResponses);
		JMenuItem mntmNewMenuItem_9 = new JMenuItem("Reload Stylesheets");
		mnTool.add(mntmNewMenuItem_9);
		JMenuItem mntmReloadTemplates = new JMenuItem("Reload Templates");
		mnTool.add(mntmReloadTemplates);
		JMenu mnCaptureScreenshot = new JMenu("Capture Screenshot");
		mnTool.add(mnCaptureScreenshot);
		JMenuItem menuItem = new JMenuItem("100%");
		mnCaptureScreenshot.add(menuItem);
		JMenuItem menuItem_1 = new JMenuItem("50%");
		mnCaptureScreenshot.add(menuItem_1);
		JMenuItem menuItem_2 = new JMenuItem("25%");
		mnCaptureScreenshot.add(menuItem_2);
		JMenuItem mntmEditCurrrentScreen = new JMenuItem(
				"Edit Currrent Page Source                           Ctrl-J");
		mnTool.add(mntmEditCurrrentScreen);
		JMenuItem mntmEditAndMonitor = new JMenuItem(
				"Edit and Monitor Page Source                    Ctrl-Shift-J");
		mnTool.add(mntmEditAndMonitor);
		JMenuItem mntmAddAssertionAnd = new JMenuItem(
				"Add Assertion and Selection                       Ctrl-Alt-A");
		mnTool.add(mntmAddAssertionAnd);
		JMenuItem mntmAddInverseAssertion = new JMenuItem(
				"Add Inverse Assertion                                  Ctrl-Alt-Shift-A");
		mnTool.add(mntmAddInverseAssertion);
		JMenuItem mntmAddVariableSetter = new JMenuItem(
				"Add Variable Setter and Selection              Ctrl-Alt-Shift-V");
		mnTool.add(mntmAddVariableSetter);
		JMenuItem mntmScheduleExcution = new JMenuItem("Schedule Excution...");
		mnTool.add(mntmScheduleExcution);		
		menuBar.add(mnPreferences);
		JMenuItem mntmPreferences = new JMenuItem(
				"Preferences                   Ctril-Shift-P            ");
		mnPreferences.add(mntmPreferences);
		menuBar.add(mnHepl);
		JMenuItem mntmContents = new JMenuItem("Contents");
		mnHepl.add(mntmContents);
		JMenuItem mntmTutorial = new JMenuItem("Tutorial");
		mnHepl.add(mntmTutorial);
		JMenuItem mntmAboutBadboy = new JMenuItem("About BadBoy");
		mnHepl.add(mntmAboutBadboy);
	}
	/**
	 * Expand TreeView
	 * 
	 * @param tree
	 */
	public void expandAll(JTree tree) {
		int row = 0;
		while (row < tree.getRowCount()) {
			tree.expandRow(row);
			row++;
		}
	}
}
